package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServiceImpl extends BaseServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Autowired
    private AdChannelMapper adChannelMapper;

  @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.参数检测
        if(dto==null)return errorParam;

        return getPageData(dto, Wrappers.<AdChannel>lambdaQuery().like(StringUtils.isNotEmpty(dto.getName()),AdChannel::getName,dto.getName()));
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.检查参数
        if(null == adChannel)return errorParam;

        //2.保存
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return success;
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        //1.检查参数
        if(null == adChannel || adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(adChannel);
        return success;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if(id == null)return errorParam;
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null)return noData;
        if(adChannel.getStatus())return zha;

        //        int i = 10/0;

        //3.删除频道
        removeById(id);
        return deleteImpl;
    }

    @Override
    public List<AdChannel> findAll() {
        return adChannelMapper.selectList(null);
    }
}
